<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="商品名称" prop="name">
              <el-input
                v-model.trim="form.name"
                placeholder="商品名称"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="分类" prop="category_id" class="wid220">
              <el-cascader
                v-model="form.category_id"
                :options="options"
                :props="defaultProps"
              ></el-cascader>
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="$refs['userListFrom'].resetFields()"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
          >
            <el-table-column label="序号">
              <template slot-scope="scope">{{
                (current.page - 1) * current.per_page + scope.$index + 1
              }}</template>
            </el-table-column>
            <el-table-column label="名称" prop="name" class-name="main_hei">
              <template slot-scope="scope">{{ scope.row.name }}</template>
            </el-table-column>
            <el-table-column label="别名" prop="byname" class-name="main_hei" />
            <el-table-column label="图片" class-name="main_hei">
              <template slot-scope="scope">
                <div class="demo-image__error mainor_im">
                  <el-image
                    fit="fill"
                    :src="scope.row.pic"
                    :preview-src-list="scope.row.pics"
                  >
                    <div slot="error" class="image-slot">
                      <img :src="puzzleImg" />
                    </div>
                  </el-image>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="分类"
              prop="userNickname"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope"
                >{{ scope.row.category_p ? scope.row.category_p.title : "" }}|{{
                  scope.row.category ? scope.row.category.title : ""
                }}</template
              >
            </el-table-column>
            <el-table-column
              label="编码"
              prop="goods_code"
              class-name="main_hei"
            />
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="140"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link type="warning" @click="knotClick(scope.row.id)"
                  >恢复</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  recycle_goods,
  recycle_recycleGoods,
} from "@/api/global/globalRecycle";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { puzzleImg } from "@/api/currency";
import { recursion, category } from "@/utils/index";

export default {
  computed: {
    ...mapGetters(["avatar"]),
  },
  components: {},
  data() {
    return {
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
      },
      puzzleImg,
      listLoading: false,
      // 搜索
      form: {
        name: "",
        pid: "",
        category_id: [],
      },
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
    };
  },
  created() {
    // 列表
    // this.recycle_goods();
  },
  methods: {
    // 恢复
    knotClick(id) {
      let data = Object.assign(
        apiUrl().name,
        { id: id }
      );
      recycle_recycleGoods(data).then((res) => {
        if (res.errcode == 0) {
          this.$message({
            message: "恢复成功",
            type: "success",
          });
          this.recycle_goods();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.recycle_goods();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.recycle_goods();
    },
    changePage(page) {
      this.current.page = page;
      this.recycle_goods();
    },
    // 列表
    recycle_goods() {
      this.listLoading = true;
      let data = Object.assign(
        apiUrl().name,
        this.list_form(),
        this.current
      );
      recycle_goods(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.options = recursion(res.data.categoryList, true);
          res.data.list.data.map((e) => {
            if (e.images.length != 0) {
              e.images.map((a) => {
                a.path = this.avatar.domain + a.path;
                if (a.main == 1) {
                  e.pic = a.path;
                }
              });
              e.pics = e.images.map((s) => s.path);
            } else {
              e.pic = "";
              e.pics = [];
            }
          });
          this.list = res.data.list;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.pid = form.category_id[0] ? form.category_id[0] : "";
      form.category_id = category(form.category_id);
      return form;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 160px);
  }
}
</style>
